package com.viphuli.app.tool.http;

import android.app.Activity;

import com.loopj.android.http.RequestParams;
import com.offroader.R;
import com.offroader.http.HttpHelper;
import com.offroader.http.MyJsonHttpResponseHandler;
import com.offroader.utils.LogUtils;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.common.AppConfig;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.handler.AccountLoginResponseHandler;
import com.viphuli.app.tool.handler.AccountLoginVCodeResponseHandler;
import com.viphuli.app.tool.handler.AccountRemindInfoResponseHandler;
import com.viphuli.app.tool.handler.AccountResponseHandler;
import com.viphuli.app.tool.handler.AccountUserTypeResponseHandler;
import com.viphuli.app.tool.handler.ArrangeActionExchangeResponseHandler;
import com.viphuli.app.tool.handler.ArrangeActionReplaceResponseHandler;
import com.viphuli.app.tool.handler.ArrangeActionResponseHandler;
import com.viphuli.app.tool.handler.ArrangeActionTypeResponseHandler;
import com.viphuli.app.tool.handler.ArrangeActionUserResponseHandler;
import com.viphuli.app.tool.handler.ArrangeCircleMemberResponseHandler;
import com.viphuli.app.tool.handler.ArrangeMonthViewResponseHandler;
import com.viphuli.app.tool.handler.ArrangeRecordDetailArrangeResponseHandler;
import com.viphuli.app.tool.handler.ArrangeRecordDetailArrangeWeekViewResponseHandler;
import com.viphuli.app.tool.handler.ArrangeRecordDetailExchangeResponseHandler;
import com.viphuli.app.tool.handler.ArrangeRecordResponseHandler;
import com.viphuli.app.tool.handler.ArrangeReplaceArrangeTypeListResponseHandler;
import com.viphuli.app.tool.handler.ArrangeTypeInfoResponseHandler;
import com.viphuli.app.tool.handler.ArrangeTypeListResponseHandler;
import com.viphuli.app.tool.handler.ArrangeWeekViewResponseHandler;
import com.viphuli.app.tool.handler.CircleCreateHandler;
import com.viphuli.app.tool.handler.CircleDepartmentListResponseHandler;
import com.viphuli.app.tool.handler.CircleExitHandler;
import com.viphuli.app.tool.handler.CircleHospitalListResponseHandler;
import com.viphuli.app.tool.handler.CircleInfoResponseHandler;
import com.viphuli.app.tool.handler.CircleMemberResponseHandler;
import com.viphuli.app.tool.handler.CircleUserAddWechatResponseHandler;
import com.viphuli.app.tool.handler.CircleUserDelHandler;
import com.viphuli.app.tool.handler.CircleWecahtShareResponseHandler;
import com.viphuli.app.tool.handler.CommonCallbackResponseHandler;
import com.viphuli.app.tool.handler.CommonMsgFinishResponseHandler;
import com.viphuli.app.tool.handler.CommonMsgRefreashResponseHandler;
import com.viphuli.app.tool.handler.CommonMsgResponseHandler;
import com.viphuli.app.tool.handler.HolidayHomeResponseHandler;
import com.viphuli.app.tool.handler.HolidayLeaveListResponseHandler;
import com.viphuli.app.tool.handler.HolidayLeaveTypeListResponseHandler;
import com.viphuli.app.tool.handler.HolidayOverWorkTypeListResponseHandler;
import com.viphuli.app.tool.handler.HomeResponseHandler;
import com.viphuli.app.tool.handler.MyBaseHttpResponseHandler;
import com.viphuli.app.tool.handler.MyBaseHttpResponseHandler.HandlerType;

/**
 * APP接口请求
 * 
 * @author li.li
 *
 */
public enum ApiRequest {

	accountLogin("登录", "/account/user/login/info", ApiTypeEnum.get, HandlerType.waitting, AccountLoginResponseHandler.class), //

	accountWechatLogin("微信登录", "/account/user/openlogin/info", ApiTypeEnum.get, HandlerType.waitting, AccountLoginResponseHandler.class), //

	accountLoginVcode("获得验证码", "/account/user/login/v_code", ApiTypeEnum.get, HandlerType.waitting, AccountLoginVCodeResponseHandler.class), //

	accountLoginVcodeVoice("语音获得验证码", "/account/user/login/v_code/voice", ApiTypeEnum.get, HandlerType.waitting, CommonMsgResponseHandler.class), //

	accountUserAgreement("用户协议", "/account/user/agreement", ApiTypeEnum.h5), //

	account("我的", "/account/user/info", ApiTypeEnum.get, HandlerType.loading, AccountResponseHandler.class),

	accountAddUserType("身份信息添加", "/account/user/info/modify", ApiTypeEnum.post, HandlerType.loading, CommonMsgFinishResponseHandler.class),

	accountChangeUserType("用户信息修改", "/account/user/info/modify", ApiTypeEnum.post, HandlerType.waitting, AccountUserTypeResponseHandler.class),

	accountChangeUserInfo("身份信息修改", "/account/user/info/modify", ApiTypeEnum.post, HandlerType.waitting, CommonMsgFinishResponseHandler.class),

	accountRemindInfo("获得提醒信息", "/account/user/setting/remind/info", ApiTypeEnum.get, HandlerType.loading, AccountRemindInfoResponseHandler.class),

	accountChangeRemindInfo("更改提醒信息", "/account/user/setting/remind/info/change", ApiTypeEnum.post, HandlerType.waitting,
			AccountRemindInfoResponseHandler.class),

	accountBindWechat("绑定微信", "/account/user/bind/wechat", ApiTypeEnum.get, HandlerType.waitting, CommonMsgResponseHandler.class),
	
	accountBindAndrShareWechat("绑定并微信分享", "/account/user/bind/wechat", ApiTypeEnum.get, HandlerType.waitting, CommonCallbackResponseHandler.class),
	
	accountBindPhone("绑定手机号", "/account/user/bind/phone", ApiTypeEnum.get, HandlerType.waitting, CommonMsgFinishResponseHandler.class),

	accountFeedback("意见反馈", "/account/feedback", ApiTypeEnum.post, HandlerType.waitting, CommonMsgResponseHandler.class),
	
	circleCreate("建立小组", "/circle/create", ApiTypeEnum.post, HandlerType.loading, CircleCreateHandler.class),

	circleInfo("小组信息", "/circle/info", ApiTypeEnum.get, HandlerType.loading, CircleInfoResponseHandler.class),

	circleExit("删除并推出小组", "/circle/exit", ApiTypeEnum.get, HandlerType.loading, CircleExitHandler.class),

	circleInfoChange("修改小组信息", "/circle/info/change", ApiTypeEnum.post, HandlerType.waitting, CommonMsgResponseHandler.class),

	circleWecahtShare("获得微信分享信息", "/circle/wechat/share/info", ApiTypeEnum.get, HandlerType.waitting, CircleWecahtShareResponseHandler.class),

	circleMember("小组成员", "/circle/info", ApiTypeEnum.get, HandlerType.loading, CircleMemberResponseHandler.class),

	circleUserAddWechat("获得小组信息（微信添加）", "/circle/info", ApiTypeEnum.get, HandlerType.loading, CircleUserAddWechatResponseHandler.class),

	circleUserAddManual("手动添加小组", "/circle/user/add/manual", ApiTypeEnum.get, HandlerType.loading, CommonCallbackResponseHandler.class),

	circleUserDel("圈子成员删除", "/circle/user/delete", ApiTypeEnum.get, HandlerType.loading, CircleUserDelHandler.class),
	
	circleHospitalList("圈子-获得医院信息列表", "/circle/hospital/list", ApiTypeEnum.get, HandlerType.waitting, CircleHospitalListResponseHandler.class),
	
	circleDepartmentList("圈子-获得科室信息列表", "/circle/department/list", ApiTypeEnum.get, HandlerType.waitting, CircleDepartmentListResponseHandler.class),
	
	circleHospitalAdd("圈子-添加医院信息", "/circle/hospital/add", ApiTypeEnum.post, HandlerType.waitting, CommonMsgFinishResponseHandler.class),
	
	circleDepartmentAdd("圈子-添加科室信息", "/circle/department/add", ApiTypeEnum.post, HandlerType.waitting, CommonMsgFinishResponseHandler.class),
	
	home("首页", "/arrange/home", ApiTypeEnum.get, HandlerType.loading, HomeResponseHandler.class),

	arrangeRecordList("排班记录", "/arrange/record/list", ApiTypeEnum.get, HandlerType.loading, ArrangeRecordResponseHandler.class),

	arrangeRecordDetail("排班记录明细（换班）", "/arrange/record/info", ApiTypeEnum.get, HandlerType.loading, ArrangeRecordDetailExchangeResponseHandler.class),

	arrangeRecordDetailArrange("排班记录明细（排班）", "/arrange/record/info", ApiTypeEnum.get, HandlerType.loading,
			ArrangeRecordDetailArrangeResponseHandler.class),

	arrangeRecordDetailArrangeWeekView("排班记录明细（排班 - 周视图）", "/arrange/info", ApiTypeEnum.get, HandlerType.loading,
			ArrangeRecordDetailArrangeWeekViewResponseHandler.class),

	arrangeWeekView("排班视图（周视图）", "/arrange/info", ApiTypeEnum.get, HandlerType.loading, ArrangeWeekViewResponseHandler.class),

	arrangeMonthView("排班视图（月视图）", "/arrange/info", ApiTypeEnum.get, HandlerType.loading, ArrangeMonthViewResponseHandler.class),

	arrangeAccept("接爱", "/arrange/accept", ApiTypeEnum.post, HandlerType.loading, CommonMsgFinishResponseHandler.class),

	arrangeReject("拒绝", "/arrange/reject", ApiTypeEnum.post, HandlerType.loading, CommonMsgFinishResponseHandler.class),

	arrangeCancel("取消", "/arrange/cancel", ApiTypeEnum.post, HandlerType.loading, CommonMsgFinishResponseHandler.class),

	arrangeTypeList("排班分类信息列表", "/arrange/type/info/list", ApiTypeEnum.get, HandlerType.loading, ArrangeTypeListResponseHandler.class),

	arrangeTypeInfo("排班分类信息", "/arrange/type/info", ApiTypeEnum.get, HandlerType.loading, ArrangeTypeInfoResponseHandler.class),

	arrangeTypeInfoChange("排班分类信息修改", "/arrange/type/info/change", ApiTypeEnum.post, HandlerType.waitting, CommonMsgFinishResponseHandler.class),
	
	arrangeTypeInfoAdd("排班分类信息增加", "/arrange/type/info/add", ApiTypeEnum.get, HandlerType.waitting, CommonMsgFinishResponseHandler.class),
	
	arrangeTypeInfoDel("排班分类信息删除", "/arrange/type/info/delete", ApiTypeEnum.get, HandlerType.waitting, CommonMsgRefreashResponseHandler.class),

	arrangeCircleMembers("排班 - 小组成员", "/circle/info", ApiTypeEnum.get, HandlerType.loading, ArrangeActionUserResponseHandler.class),

	arrangeActionType("排班 - 排班分类信息列表", "/arrange/type/info/list", ApiTypeEnum.get, HandlerType.loading, ArrangeActionTypeResponseHandler.class),

	arrangeAction("排班操作", "/arrange", ApiTypeEnum.get, HandlerType.loading, ArrangeActionResponseHandler.class),

	arrangeActionExchange("换班&替班操作", "/arrange", ApiTypeEnum.get, HandlerType.waitting, ArrangeActionExchangeResponseHandler.class),
	
	arrangeActionReplace("换班操作", "/arrange", ApiTypeEnum.get, HandlerType.waitting, ArrangeActionReplaceResponseHandler.class),

	arrangeActionReplaceTypeList("换班操作 - 排班分类信息列表", "/arrange/type/info/list", ApiTypeEnum.get, HandlerType.waitting, ArrangeReplaceArrangeTypeListResponseHandler.class),
	
	arrangeActionConfirm("确定排班&换班&替班操作", "/arrange/confirm", ApiTypeEnum.get, HandlerType.loading, CommonMsgFinishResponseHandler.class), 
	
	arrangeCircleMember("排班小组成员", "/circle/info", ApiTypeEnum.get, HandlerType.loading, ArrangeCircleMemberResponseHandler.class),
	
	arrangePositionExchange("排班-位置更改", "/arrange/position/exchange", ApiTypeEnum.get, HandlerType.none, CommonMsgResponseHandler.class),
	
	holidayLeaveList("假期-余量列表", "/arrange/leave/list", ApiTypeEnum.get, HandlerType.none, HolidayLeaveListResponseHandler.class),
	
	holidayLeaveSetting("假期-余量设置", "/arrange/leave/setting", ApiTypeEnum.get, HandlerType.waitting, CommonMsgResponseHandler.class),
	
	holidayOverWorkTypeList("假期-加班类型列表", "/arrange/type/info/list", ApiTypeEnum.get, HandlerType.waitting, HolidayOverWorkTypeListResponseHandler.class),
	
	holidayLeaveTypeList("假期-获取假期类型", "/arrange/type/info/list", ApiTypeEnum.get, HandlerType.waitting, HolidayLeaveTypeListResponseHandler.class),
	
	holidayAdd("假期-假期&请假添加", "/arrange/leave/add", ApiTypeEnum.get, HandlerType.waitting, CommonMsgFinishResponseHandler.class),
	
	holidayHome("假期-假期管理首页", "/arrange/leave", ApiTypeEnum.get, HandlerType.waitting, HolidayHomeResponseHandler.class),
	
	;

	/** 接口名 */
	private String name;
	/** 接口地址 */
	private String apiPath;
	/** 接口类型 */
	private ApiTypeEnum apiType;
	/** 接口结果处理器 */
	private Class<? extends MyJsonHttpResponseHandler<?, ?>> handlerClazz;
	/** 处理器类型 */
	private HandlerType handlerType;

	private ApiRequest(String name, String apiPath, ApiTypeEnum apiType, HandlerType handlerType,
			Class<? extends MyJsonHttpResponseHandler<?, ?>> handlerClazz) {
		this.apiPath = apiPath;
		this.apiType = apiType;
		this.handlerClazz = handlerClazz;
		this.name = name;
		this.handlerType = handlerType;
	}

	private ApiRequest(String name, String apiPath, ApiTypeEnum apiType, Class<? extends MyJsonHttpResponseHandler<?, ?>> handlerClazz) {
		this.apiPath = apiPath;
		this.apiType = apiType;
		this.handlerClazz = handlerClazz;
		this.name = name;
	}

	private ApiRequest(String name, String apiPath, ApiTypeEnum apiType) {
		this.apiPath = apiPath;
		this.apiType = apiType;
		this.name = name;
	}

	public <T> void request(T caller, RequestParams params, HandlerType handlerType) {
		request(caller, params, handlerType, true);
	}

	public <T> void request(T caller, RequestParams params, HandlerType handlerType, boolean showTip) {
		if (handlerClazz == null)
			return;

		String apiUrl = AppConfig.API_DOMAIN + AppConfig.API_PATH + apiPath;
		//	String apiUrl = AppConfig.API_DOMAIN;

		try {
			MyBaseHttpResponseHandler<T, ?> responseHandler = (MyBaseHttpResponseHandler<T, ?>) handlerClazz.newInstance();

			//Handler初始化
			responseHandler.init(caller);

			//设置结果处理类型
			responseHandler.setHandlerType(handlerType);

			//设置为测试模式（读本地接口数据）
			responseHandler.setTest(false);

			//是否显示toast
			responseHandler.setShowTip(showTip);

			LogUtils.info("接口请求|" + apiType.name() + "|" + apiUrl + "?" + params.toString());

			//请求拦截器
			HttpHelper.getInstance().addResponseInterceptor(new MyHttpResponseInterceptor());
			//接口类型
			if (ApiTypeEnum.get.equals(apiType))
				HttpHelper.getInstance().get(apiUrl, params, responseHandler);
			else if (ApiTypeEnum.post.equals(apiType))
				HttpHelper.getInstance().post(apiUrl, params, responseHandler);
			else
				LogUtils.error("错误", new Exception("非GET&POST接口禁止调用|" + name));

		} catch (Throwable e) {
			LogUtils.error(e.getMessage(), e);
			ViewUtils.toast(R.string.net_data_error);
		}

	}

	public <T> void request(T caller, RequestParams params) {
		request(caller, params, handlerType, true);

	}

	public <T> void request(T caller, RequestParams params, boolean showTip) {

		request(caller, params, handlerType, showTip);

	}

	/**
	 * H5页面 
	 * @param caller
	 * @param url
	 * @param title
	 */
	public void request(Activity caller, String title) {

		RequestParams params = new RequestParams();
		request(caller, params);

	}

	/**
	 * H5页面 
	 * @param caller
	 * @param url
	 * @param title
	 */
	public void request(Activity caller, RequestParams params, String title) {
		String apiUrl = AppConfig.API_DOMAIN + AppConfig.API_PATH + apiPath;

		if (params != null) {

			String p = params.toString();

			if (StringUtils.isNotBlank(p)) {
				apiUrl = apiUrl + "?" + p;
			}
		}

		LogUtils.info("接口请求|" + apiType.name() + "|" + apiUrl);

		if (ApiTypeEnum.h5.equals(apiType))
			MyPageHelper.webView.showMyWebViewPage(caller, apiUrl, title);
		else
			LogUtils.error("错误", new Exception("非H5接口禁止调用|" + name));

	}

	/**
	 * H5页面 
	 * @param caller
	 * @param url
	 */
	public void request(Activity caller) {

		request(caller, name);
	}

	/**
	 * H5页面 
	 * @param caller
	 * @param url
	 */
	public void request(Activity caller, RequestParams params) {

		request(caller, params, name);
	}

	/**
	 * 接口类型
	 * @author li.li
	 *
	 */
	private enum ApiTypeEnum {
		get, //获取数据
		post, //发送数据
		h5, //H5网址
		;
	}

}
